package com.thinkit.bigdata.web.controller.config;

import com.thinkit.bigdata.core.feature.orm.mybatis.Page;
import com.thinkit.bigdata.web.model.config.SysStatusConfig;
import com.thinkit.bigdata.web.model.config.SysStatusConfigExample;
import com.thinkit.bigdata.web.model.result.ConfigResult;
import com.thinkit.bigdata.web.model.result.Constants;
import com.thinkit.bigdata.web.service.config.StatusConfigService;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 属性状态设置
 * Created by lihongli on 2018/08/30
 */

@Controller
@RequestMapping("statusConfig")
public class StatusConfigController {

    private Logger logger = Logger.getLogger(StatusConfigController.class);

    @Resource
    private StatusConfigService statusConfigService;

    /**
     * 请求进入属性状态配置页
     *
     * @return
     */
    @RequestMapping(value = "/statusConfigs")
    public String showStatusConfigs(HttpServletRequest request) {
        logger.info("进入属性状态配置页");
        return "ConfigViews/statusConfig";
    }

    /**
     * 获取所有属性状态信息 初始化页面查询下拉框
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("/configListGet")
    public List<SysStatusConfig> configListGet(HttpServletRequest request) {
        logger.info("页面状态类型查询下拉框数据获取");
        SysStatusConfigExample example = new SysStatusConfigExample();
        example.setOrderByClause("id asc");
        return statusConfigService.selectByExample(example);
    }

    /**
     * 分页获取数据
     *
     * @param typeSearch 类型
     * @return
     */
    @ResponseBody
    @RequestMapping("/statusConfigByPage")
    public Page<SysStatusConfig> statusConfigByPage(
            HttpServletRequest request,
            @RequestParam(value = "sortOrder") String sortOrder,
            @RequestParam(value = "sortName") String sortName,
            @RequestParam(value = "typeSearch", required = false) List<Long> typeSearch,
            @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        // 查询参数打印
        String logTxt = "【属性状态配置分页查询】";
        logger.info(logTxt);
        logger.info(logTxt + "查询条件: pageNum=" + pageNum);
        logger.info(logTxt + "查询条件: pageSize=" + pageSize);
        logger.info(logTxt + "查询条件: typeSearch=" + StringUtils.join(typeSearch, ","));
        Page<SysStatusConfig> page = new Page<>(pageNum, pageSize);
        SysStatusConfigExample example = new SysStatusConfigExample();
        example.setOrderByClause(sortName + " " + sortOrder);
        if (typeSearch != null && !typeSearch.isEmpty()) {
            example.createCriteria().andIdIn(typeSearch);
        }
        statusConfigService.selectByExample(page, example);
        logger.info(logTxt + "共查询出" + page.getResult().size() + "条属性状态信息");
        return page;
    }

    /**
     * 新增类型前先检查状态类型是否存在
     *
     * @param name 状态类型
     * @return
     */
    @ResponseBody
    @RequestMapping("/nameUniqueCheck")
    public ConfigResult nameUniqueCheck(HttpServletRequest request, @RequestParam(value = "name") String name) {
        String logTxt = "【属性状态类型name唯一性验证】";
        logger.info(logTxt + "name=" + name);
        SysStatusConfigExample example = new SysStatusConfigExample();
        example.createCriteria().andNameEqualTo(name);
        int count = statusConfigService.countByExample(example);
        if (count > 0) {
            logger.info(logTxt + name + "已存在");
            return new ConfigResult(Constants.ERROR, "该状态类型已存在");
        } else {
            logger.info(logTxt + name + "校验通过");
            return new ConfigResult(Constants.SUCCESS, "√");
        }
    }

    /**
     * 新增类型前先检查字典类型是否存在
     * 暂未使用
     *
     * @param type 字典类型（暂不校验）
     * @return
     */
    @ResponseBody
    @RequestMapping("/typeUniqueCheck")
    public ConfigResult typeUniqueCheck(HttpServletRequest request, @RequestParam(value = "type") String type) {
        String logTxt = "【属性状态类型type唯一性验证】";
        logger.info(logTxt + "type=" + type);
        SysStatusConfigExample example = new SysStatusConfigExample();
        example.createCriteria().andTypeEqualTo(type);
        int count = statusConfigService.countByExample(example);
        if (count > 0) {
            logger.info(logTxt + type + "已存在");
            return new ConfigResult(Constants.ERROR, "该数据字典已存在");
        } else {
            logger.info(logTxt + type + "校验通过");
            return new ConfigResult(Constants.SUCCESS, "√");
        }
    }

    /**
     * 添加属性状态信息(id字段使用数据库自增)
     *
     * @param request
     * @param statusConfig
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/statusConfigAdd", method = RequestMethod.POST)
    public ConfigResult statusConfigAdd(HttpServletRequest request, SysStatusConfig statusConfig) {
        String logTxt = "【属性状态添加方法】";
        logger.info(logTxt + statusConfig.toString());
        // 设置写入时间
        statusConfig.setLastupdatetime(new Date());
        try {
            int rowNum = statusConfigService.insert(statusConfig);
            if (rowNum > 0) {
                logger.info(logTxt + "成功插入" + rowNum + "条属性状态信息");
                return new ConfigResult(Constants.SUCCESS, "属性状态添加成功");
            } else {
                logger.info(logTxt + "属性状态信息添加失败");
                return new ConfigResult(Constants.FAIL, "属性状态添加失败");
            }
        } catch (Exception e) {
            logger.info(logTxt + "属性状态添加错误", e);
            return new ConfigResult(Constants.ERROR, "属性状态添加错误");
        }
    }

    /**
     * 根据主键id获取属性状态信息
     *
     * @param request
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("/statusConfigGetByPK")
    public SysStatusConfig statusConfigGetByPK(HttpServletRequest request, Long id) {
        logger.info("获取id为【" + id + "】的属性状态信息");
        return statusConfigService.statusConfigGetByPK(id);
    }

    /**
     * 修改属性状态信息（type字段不允许修改）
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/statusConfigUpdate", method = RequestMethod.POST)
    public ConfigResult statusConfigUpdate(HttpServletRequest request, SysStatusConfig statusConfig) {
        String logTxt = "【属性状态修改方法】";
        logger.info(logTxt + "修改" + statusConfig.toString());
        statusConfig.setLastupdatetime(new Date());
        try {
            int rowNum = statusConfigService.update(statusConfig);
            if (rowNum > 0) {
                logger.info(logTxt + statusConfig.toString() + "属性状态修改成功");
                return new ConfigResult(Constants.SUCCESS, "属性状态修改成功");
            } else {
                logger.info(logTxt + statusConfig.toString() + "属性状态修改失败");
                return new ConfigResult(Constants.FAIL, "属性状态修改失败");
            }
        } catch (Exception e) {
            logger.info(logTxt + "属性状态修改出现错误", e);
            return new ConfigResult(Constants.ERROR, "属性状态修改出现错误");
        }
    }

    /**
     * 删除属性状态信息
     *
     * @param request
     * @param statusConfig 该对象中必须包含id和type信息
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/statusConfigDelete", method = RequestMethod.POST)
    public ConfigResult statusConfigDelete(HttpServletRequest request, SysStatusConfig statusConfig) {
        String logTxt = "【属性状态删除方法】";
        logger.info(logTxt + "删除" + statusConfig.toString());
        try {
            int rowNum = statusConfigService.delete(statusConfig);
            if (rowNum > 0) {
                logger.info(logTxt + statusConfig.toString() + "删除成功");
                return new ConfigResult(Constants.SUCCESS, "属性状态删除成功");
            } else {
                logger.info(logTxt + statusConfig.toString() + "删除失败");
                return new ConfigResult(Constants.FAIL, "属性状态删除失败");
            }
        } catch (Exception e) {
            logger.info(logTxt + "属性状态信息删除错误", e);
            return new ConfigResult(Constants.ERROR, "属性状态信息删除错误");
        }
    }
}
